package com.lxb.myforum.service

import java.util.HashMap

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

import com.lxb.myforum.pojo.Star
import com.lxb.myforum.pojo.StarType
import com.lxb.myforum.pojo.User
import com.lxb.myforum.repository.StarRepository

@Service
class StarService @Autowired constructor(private val starRepository: StarRepository) {

    /**
     * 收藏或者取消收藏
     *
     * @return
     */
    fun star(star: Star): Map<String, Any> {
        val result = HashMap<String, Any>()
        var tmpStar: Star? = starRepository.findOneByTargetIdAndTypeAndUser(star.targetId!!, star.type!!, star.user!!)

        if (tmpStar == null) {
            result["action"] = 1 // 添加收藏
            tmpStar = starRepository.save(star)
        } else {
            result["action"] = -1 // 取消收藏
            starRepository.deleteById(tmpStar.id!!)
        }
        result["star"] = tmpStar
        return result
    }

    /**
     * 判断用户u 收藏了类型为type，id为targetIds的目标
     *
     * @param u
     * @param type
     * @param targetIds
     */
    fun checkStar(u: User, type: StarType, targetIds: List<Long>): List<Star> {
        return starRepository.checkStar(type, u, targetIds)
    }
}
